package com.nys.cqhttp.message.factory.impl;

import com.nys.cqhttp.mapper.MessageLogMapper;
import com.nys.cqhttp.mapper.SysUserMapper;
import com.nys.cqhttp.mapper.UserBackpackMapper;
import com.nys.cqhttp.message.factory.IMessageHandler;
import com.nys.cqhttp.model.*;
import com.nys.cqhttp.param.Context;
import com.nys.cqhttp.param.MessageParam;
import com.nys.cqhttp.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * DefaultMessageHandler
 *
 * @author nysheng
 * @version 1.0.0
 * @description 消息默认处理
 * @date 2021/12/28 15:44
 */
@Slf4j
@Component
public class UserMessageHandler implements IMessageHandler {
  @Autowired private SysUserMapper sysUserMapper;
  @Autowired private MessageLogMapper messageLogMapper;
  @Autowired private UserBackpackMapper userBackpackMapper;

  private static UserMessageHandler userMessageHandler;
  private static String at = "[CQ:at,qq=";
  private static String at1 = "]";

  @PostConstruct
  public void init() throws Exception {
    userMessageHandler = this;
    sysUserMapper = this.sysUserMapper;
    messageLogMapper = this.messageLogMapper;
    userBackpackMapper = this.userBackpackMapper;
  }

  @Override
  public List<String> listHandler(Massage messagePojo, Context context) {
    return Arrays.asList(handler(messagePojo, context));
  }

  @Override
  public String handler(Massage messagePojo, Context context) {
    MessageParam messageParam = context.getMessageParam();
    if (messageParam.getMessage().equals("签到")) {
      MessageLogExample messageLogExample = new MessageLogExample();
      MessageLogExample.Criteria criteria1 = messageLogExample.createCriteria();
      criteria1.andMessageEqualTo(messageParam.getMessage());
      criteria1.andJqrQqEqualTo(messageParam.getSelfId());
      criteria1.andUserQqEqualTo(messageParam.getSenderId());
      criteria1.andSendDateGreaterThan(DateUtils.getNowDayStart());
      List<MessageLog> messageLogs =
          userMessageHandler.messageLogMapper.selectByExample(messageLogExample);
      if (CollectionUtils.isEmpty(messageLogs)) {
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andJqrQqEqualTo(messageParam.getSelfId());
        criteria.andUserQqEqualTo(messageParam.getSenderId());
        List<SysUser> sysUsers = userMessageHandler.sysUserMapper.selectByExample(sysUserExample);
        int i =
            userMessageHandler.messageLogMapper.selectByBZ(
                    messageParam.getSenderId(), messageParam.getSelfId())
                + 1;
        int num = 10 * i;
        //        num =100;
        int num1 = 0;
        if (i == 7) {
          Random random = new Random();
          num1 = random.nextInt(99) + 1;
        }
        if (CollectionUtils.isEmpty(sysUsers)) {
          SysUser sysUser = new SysUser();
          sysUser.setJqrQq(messageParam.getSelfId());
          sysUser.setUserQq(messageParam.getSenderId());
          sysUser.setGold(num);
          userMessageHandler.sysUserMapper.insertSelective(sysUser);
        } else {
          SysUser sysUser = sysUsers.get(0);
          sysUser.setGold(sysUser.getGold() + num + num1);
          sysUser.setSpirit(sysUser.getSpirit() + 5 > 100 ? 100 : sysUser.getSpirit() + 5);
          userMessageHandler.sysUserMapper.updateByPrimaryKeySelective(sysUser);
        }
        MessageLog messageLog = new MessageLog();
        messageLog.setMessage(messageParam.getMessage());
        messageLog.setUserQq(messageParam.getSenderId());
        messageLog.setJqrQq(messageParam.getSelfId());
        messageLog.setSendDate(DateUtils.getNowDate());
        userMessageHandler.messageLogMapper.insertSelective(messageLog);

        return at
            + messageParam.getSenderId()
            + at1
            + "签到成功,本周已签到"
            + i
            + "天(周签到满有额外1~100金币),获取"
            + num
            + "金币"
            + (num1 > 0 ? ",额外获得" + num1 + "金币" : "");
      } else {
        return at
            + messageParam.getSenderId()
            + at1
            + "今天"
            + DateUtils.format(messageLogs.get(0).getSendDate(), DateUtils.HH_MM_SS)
            + "已经签到过了哦";
      }
    } else if (messageParam.getMessage().equals("抽奖")) {
      MessageLogExample messageLogExample = new MessageLogExample();
      MessageLogExample.Criteria criteria1 = messageLogExample.createCriteria();
      criteria1.andMessageEqualTo(messageParam.getMessage());
      criteria1.andJqrQqEqualTo(messageParam.getSelfId());
      criteria1.andUserQqEqualTo(messageParam.getSenderId());
      criteria1.andSendDateGreaterThan(DateUtils.getNowDayStart());

      List<MessageLog> messageLogs =
          userMessageHandler.messageLogMapper.selectByExample(messageLogExample);
      if (CollectionUtils.isEmpty(messageLogs)) {
        Random random = new Random();
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andJqrQqEqualTo(messageParam.getSelfId());
        criteria.andUserQqEqualTo(messageParam.getSenderId());
        List<SysUser> sysUsers = userMessageHandler.sysUserMapper.selectByExample(sysUserExample);
        SysUser sysUser = new SysUser();
        if (CollectionUtils.isEmpty(sysUsers)) {
          sysUser = new SysUser();
          sysUser.setJqrQq(messageParam.getSelfId());
          sysUser.setUserQq(messageParam.getSenderId());
          userMessageHandler.sysUserMapper.insertSelective(sysUser);
          sysUsers = userMessageHandler.sysUserMapper.selectByExample(sysUserExample);
        }
        sysUser = sysUsers.get(0);
        int i1 = sysUser.getMilitaryStrength()>=100000?sysUser.getGold()>0?random.nextInt(4) :4:4;
        if (i1 > 0) {
          int i = random.nextInt(99) + 1;

          sysUser.setGold(sysUser.getGold() + i);
          userMessageHandler.sysUserMapper.updateByPrimaryKeySelective(sysUser);

          MessageLog messageLog = new MessageLog();
          messageLog.setMessage(messageParam.getMessage());
          messageLog.setUserQq(messageParam.getSenderId());
          messageLog.setJqrQq(messageParam.getSelfId());
          messageLog.setSendDate(new Date());
          userMessageHandler.messageLogMapper.insertSelective(messageLog);
          return at + messageParam.getSenderId() + at1 + "恭喜你，中奖" + i + "个金币";
        } else {
          MessageLog messageLog = new MessageLog();
          messageLog.setMessage(messageParam.getMessage());
          messageLog.setUserQq(messageParam.getSenderId());
          messageLog.setJqrQq(messageParam.getSelfId());
          messageLog.setSendDate(new Date());
          userMessageHandler.messageLogMapper.insertSelective(messageLog);
          return at + messageParam.getSenderId() + at1 + "很遗憾没有中奖";
        }
      } else {
        return at
            + messageParam.getSenderId()
            + at1
            + "今天"
            + DateUtils.format(messageLogs.get(0).getSendDate(), DateUtils.HH_MM_SS)
            + "已经抽奖过了哦";
      }
    } else if (messageParam.getMessage().equals("金币")) {
      SysUserExample sysUserExample = new SysUserExample();
      SysUserExample.Criteria criteria = sysUserExample.createCriteria();
      criteria.andJqrQqEqualTo(messageParam.getSelfId());
      criteria.andUserQqEqualTo(messageParam.getSenderId());
      List<SysUser> sysUsers = userMessageHandler.sysUserMapper.selectByExample(sysUserExample);
      if (CollectionUtils.isEmpty(sysUsers)) {
        SysUser sysUser = new SysUser();
        sysUser.setJqrQq(messageParam.getSelfId());
        sysUser.setUserQq(messageParam.getSenderId());
        sysUser.setGold(0);
        userMessageHandler.sysUserMapper.insertSelective(sysUser);
        return at + messageParam.getSenderId() + at1 + "你的金币为0个";
      } else {
        return at + messageParam.getSenderId() + at1 + "你的金币为" + sysUsers.get(0).getGold() + "个";
      }
    } else if (messageParam.getMessage().equals("背包")) {
      SysUserExample sysUserExample = new SysUserExample();
      SysUserExample.Criteria criteria = sysUserExample.createCriteria();
      criteria.andJqrQqEqualTo(messageParam.getSelfId());
      criteria.andUserQqEqualTo(messageParam.getSenderId());
      List<SysUser> sysUsers = userMessageHandler.sysUserMapper.selectByExample(sysUserExample);
      if (CollectionUtils.isEmpty(sysUsers)) {
        SysUser sysUser = new SysUser();
        sysUser.setJqrQq(messageParam.getSelfId());
        sysUser.setUserQq(messageParam.getSenderId());
        sysUser.setGold(0);
        userMessageHandler.sysUserMapper.insertSelective(sysUser);
      }
      UserBackpackExample userBackpackExample = new UserBackpackExample();
      UserBackpackExample.Criteria criteria1 = userBackpackExample.createCriteria();
      criteria1.andJqrQqEqualTo(messageParam.getSelfId());
      criteria1.andUserQqEqualTo(messageParam.getSenderId());
      List<UserBackpack> userBackpacks =
          userMessageHandler.userBackpackMapper.selectByExample(userBackpackExample);
      UserBackpack userBackpack = new UserBackpack();

      if (CollectionUtils.isEmpty(userBackpacks)) {
        userBackpack.setJqrQq(messageParam.getSelfId());
        userBackpack.setUserQq(messageParam.getSenderId());
        userBackpack.setBoutiqueBait(0);
        userBackpack.setPremiumBait(0);
        userBackpack.setPlainBait(0);
        userMessageHandler.userBackpackMapper.insertSelective(userBackpack);
      } else {
        userBackpack = userBackpacks.get(0);
      }
      return at
          + messageParam.getSenderId()
          + at1
          + "\n你的背包如下:\n普通鱼饵:"
          + userBackpack.getPlainBait()
          + "(脱钩率50%)\n优质鱼饵:"
          + userBackpack.getPremiumBait()
          + "(脱钩率20%)\n精品鱼饵:"
          + userBackpack.getBoutiqueBait()
          + "(脱钩率0%)";
    } else if (messageParam.getMessage().equals("新年快乐")) {
      MessageLogExample messageLogExample = new MessageLogExample();
      MessageLogExample.Criteria criteria1 = messageLogExample.createCriteria();
      criteria1.andMessageEqualTo(messageParam.getMessage());
      criteria1.andJqrQqEqualTo(messageParam.getSelfId());
      criteria1.andUserQqEqualTo(messageParam.getSenderId());
      criteria1.andSendDateGreaterThan(DateUtils.getNowDayStart());

      List<MessageLog> messageLogs =
          userMessageHandler.messageLogMapper.selectByExample(messageLogExample);
      if (CollectionUtils.isEmpty(messageLogs)) {
        Random random = new Random();
        int i = random.nextInt(999) + 1;
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andJqrQqEqualTo(messageParam.getSelfId());
        criteria.andUserQqEqualTo(messageParam.getSenderId());
        List<SysUser> sysUsers = userMessageHandler.sysUserMapper.selectByExample(sysUserExample);
        if (CollectionUtils.isEmpty(sysUsers)) {
          SysUser sysUser = new SysUser();
          sysUser.setJqrQq(messageParam.getSelfId());
          sysUser.setUserQq(messageParam.getSenderId());
          sysUser.setGold(i);
          userMessageHandler.sysUserMapper.insertSelective(sysUser);
        } else {
          SysUser sysUser = sysUsers.get(0);
          sysUser.setGold(sysUser.getGold() + i);
          userMessageHandler.sysUserMapper.updateByPrimaryKeySelective(sysUser);
        }
        MessageLog messageLog = new MessageLog();
        messageLog.setMessage(messageParam.getMessage());
        messageLog.setUserQq(messageParam.getSenderId());
        messageLog.setJqrQq(messageParam.getSelfId());
        messageLog.setSendDate(new Date());
        userMessageHandler.messageLogMapper.insertSelective(messageLog);
        return at + messageParam.getSenderId() + at1 + "新年快乐啊！这是我给你的新年红包，" + i + "金币";
      } else {
        return at + messageParam.getSenderId() + at1 + "新年快乐啊！没红包了哦";
      }
    } else if (messageParam.getMessage().equals("除夕快乐")) {
      MessageLogExample messageLogExample = new MessageLogExample();
      MessageLogExample.Criteria criteria1 = messageLogExample.createCriteria();
      criteria1.andMessageEqualTo(messageParam.getMessage());
      criteria1.andJqrQqEqualTo(messageParam.getSelfId());
      criteria1.andUserQqEqualTo(messageParam.getSenderId());
      criteria1.andSendDateGreaterThan(DateUtils.getNowDayStart());

      List<MessageLog> messageLogs =
          userMessageHandler.messageLogMapper.selectByExample(messageLogExample);
      if (CollectionUtils.isEmpty(messageLogs)) {
        Random random = new Random();
        int i = random.nextInt(999) + 1;
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andJqrQqEqualTo(messageParam.getSelfId());
        criteria.andUserQqEqualTo(messageParam.getSenderId());
        List<SysUser> sysUsers = userMessageHandler.sysUserMapper.selectByExample(sysUserExample);
        if (CollectionUtils.isEmpty(sysUsers)) {
          SysUser sysUser = new SysUser();
          sysUser.setJqrQq(messageParam.getSelfId());
          sysUser.setUserQq(messageParam.getSenderId());
          sysUser.setGold(i);
          userMessageHandler.sysUserMapper.insertSelective(sysUser);
        } else {
          SysUser sysUser = sysUsers.get(0);
          sysUser.setGold(sysUser.getGold() + i);
          userMessageHandler.sysUserMapper.updateByPrimaryKeySelective(sysUser);
        }
        MessageLog messageLog = new MessageLog();
        messageLog.setMessage(messageParam.getMessage());
        messageLog.setUserQq(messageParam.getSenderId());
        messageLog.setJqrQq(messageParam.getSelfId());
        messageLog.setSendDate(new Date());
        userMessageHandler.messageLogMapper.insertSelective(messageLog);
        return at + messageParam.getSenderId() + at1 + "除夕快乐啊！这是我给你的除夕红包，" + i + "金币";
      } else {
        return at + messageParam.getSenderId() + at1 + "除夕快乐啊！没红包了哦";
      }
    }
    return null;
  }
}
